package othello.model;
import java.util.List;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Queue;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import othello.util.*;
	
public class TrainingMovesFactory {
	
	private Queue<SimpleGameBoard> d_boards1 = new LinkedList<SimpleGameBoard>();
	private Queue<SimpleGameBoard> d_boards2 = new LinkedList<SimpleGameBoard>();
	
	public TrainingMovesFactory(GameDetails g) {
		
		d_boards1.add(new SimpleGameBoard(8, 8, g));
		SimpleGameBoard tmp = new SimpleGameBoard(8, 8, g);		
		tmp.nextTurn();
		d_boards2.add(tmp);
	}
	
	public List<byte[][]> getStartingPositionsWhite() {
		System.out.println("Positions 1: " + d_boards1.peek().getCurrentPlayerColor() + " starts.");
		return getTrainingMoves(d_boards1);
	}
	
	public List<byte[][]> getStartingPositionsGray() {
		System.out.println("Positions 2: " + d_boards2.peek().getCurrentPlayerColor() + " starts.");
		return getTrainingMoves(d_boards2);
	}
	
	private List<byte[][]> getTrainingMoves(Queue<SimpleGameBoard> d_boards) {
		int count = 0;
		int turns = 0;		
		while (d_boards.peek().getTurns() < 4) {						
			SimpleGameBoard currBoard =  d_boards.poll();			
			List<Action> availActions = currBoard.getAvailableActions();   		
    		
    		if (availActions == null)
				System.out.println("no available moves");
    		
    		for (Action a : availActions)
    		{
    			SimpleGameBoard hypoBoard = currBoard.doPlacePiece(a.getRow(), a.getCol());    			
				d_boards.add(hypoBoard);
	    	} 	    	
		}
		
		List<SimpleGameBoard> memory = new ArrayList<SimpleGameBoard>();
		List<byte[][]> starts = new ArrayList<byte[][]>();
		for (SimpleGameBoard b : d_boards) {
			if (!memory.contains(b)) {
				starts.add(b.getState());
				memory.add(b);
			}
		}	
		
			
		System.out.println("Generated " + starts.size() + " possible starting positions.");	    		
		
		
		return starts;
	}
}
	
	
	
	
	
